/**
 * 时间工具类
 * @author czp
 * date 2018-04-01
 */
// 时间格式化
Date.prototype.format = function(fmt) {
    var o = {
        "M+" : this.getMonth() + 1,
        "d+" : this.getDate(),
        "h+" : this.getHours(),
        "m+" : this.getMinutes(),
        "s+" : this.getSeconds(),
        "q+" : Math.floor((this.getMonth()+3) / 3),
        "S"  : this.getMilliseconds()
    };
    if(/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    for(var k in o) {
        if(new RegExp("("+ k +")").test(fmt)){
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
        }
    }
    return fmt;
};

// 两个日期之间相差几天
function getInervalDay(startDay, endDay) {
    var ms = endDay.getTime() - startDay.getTime();
    if (ms < 0) return 0;
    return Math.floor(ms/(24*3600*1000));
}

// 打印一段时间 day
function printDay(timeRange, splitTag) {
    var rStrBuf = [];
    var strBuf = timeRange.split(splitTag);
    var day = getInervalDay(new Date(strBuf[0].trim()), new Date(strBuf[strBuf.length -1].trim()));
    if (day === 0) rStrBuf[0] = new Date().format("yyyy-MM-dd");
    for (var i = 0, j = day - 1; i < day; i++, j--) {
       rStrBuf[i] = new Date( new Date() - j*24*3600*1000 ).format("yyyy-MM-dd");
    }
    return rStrBuf;
}

// 打印一段时间 Second
function printSecond(timeRange, splitTag) {
    var rStrBuf = [];
    var strBuf = timeRange.split(splitTag);
    var day = getInervalDay(new Date(strBuf[0].trim()), new Date(strBuf[strBuf.length -1].trim()));
    if (day === 0) rStrBuf[0] = new Date().format("yyyy-MM-dd HH:mm:ss");
    for (var i = 0, j = day - 1; i < day; i++, j--) {
        rStrBuf[i] = new Date( new Date() - j*24*3600*1000 ).format("yyyy-MM-dd HH:mm:ss");
    }
    return rStrBuf;
}
